Skip to main content

V2Router02

V2Router02 is used as a intermediate contract to interact with liquidity pools, or the lower level V2Pair contract. The contract can be used to swap, add liquidity, withdraw liquidity. The contract also contains many variations of these three tasks that can be used for each unique situation like using native gas tokens (i.e. ETH), and Fee-On-Transfer tokens with a tax when transfers happen.

The full contract can be found here.

Read-Only Functions

factory

function factory() external pure returns (address);

Returns factory address.

WETH

function WETH() external pure returns (address);

Returns the canonical WETH address on the Ethereum mainnet, or the canonical native wrapped address for the network the contracts are deployed on.

quote

See quote.

getAmountOut

See getAmountOut.

getAmountIn

See getAmountIn.

getAmountsOut

function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts);

See getAmountsOut.

getAmountsIn

function getAmountsIn(uint amountOut, address[] memory path) public view returns (uint[] memory amounts);

See getAmountsIn.

State-Changing Functions

addLiquidity

function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);

Adds liquidity to an ERC-20⇄ERC-20 pool.

  • To cover all possible scenarios, msg.sender should have already given the router an allowance of at least amountADesired/amountBDesired on tokenA/tokenB.
  • Always adds assets at the ideal ratio, according to the price when the transaction is executed.
  • If a pool for the passed tokens does not exists, one is created automatically, and exactly amountADesired/amountBDesired tokens are added.
  • amountAMin and amountBMin can be used for slippage protection.
  • deadline is used to set a time restriction on how long it can take for the tx to be executed.

Parameters

NameTypeDescription
tokenAaddressaddress for one of the tokens in the pair
tokenBaddressaddress for the other token in the pair
amountADesireduintamount of tokenA to add as liquidity if the B/A price is <= amountBDesired/amountADesired (A depreciates)
amountBDesireduintamount of tokenB to add as liquidity if the A/B price is <= amountADesired/amountBDesired (B depreciates)
amountAMinuintbounds the extent to which the B/A price can go up before the transaction reverts. Must be <= amountADesired
amountBMinuintbounds the extent to which the A/B price can go up before the transaction reverts. Must be <= amountBDesired
toaddressrecipient of the liquidity tokens
deadlineuintunix timestamp after which the transaction will revert

Returns

NameTypeDescription
amountAuintamount of tokenA added to the pair
amountBuintamount of tokenB added to the pair
liquidityuintamount of liquidity tokens minted

addLiquidityETH

function addLiquidityETH(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external payable returns (uint amountToken, uint amountETH, uint liquidity);

Adds liquidity to an ERC-20⇄WETH pool with ETH.

  • To cover all possible scenarios, msg.sender should have already given the router an allowance of at least amountTokenDesired on token.
  • Always adds assets at the ideal ratio, according to the price when the transaction is executed.
  • msg.value is treated as a amountETHDesired.
  • Leftover ETH, if any, is returned to msg.sender.
  • If a pool for the passed token and WETH does not exists, one is created automatically, and exactly amountTokenDesired/msg.value tokens are added.
  • amountTokenMin and amountETHMin can be used for slippage protection.
  • deadline is used to set a time restriction on how long it can take for the tx to be executed.

Parameters

NameTypeDescription
tokenaddressother token besides WETH in the pair
amountTokenDesireduintamount of token to add as liquidity if the WETH/token price is <= msg.value/amountTokenDesired (token depreciates)
amountTokenMinuintamount of ETH to add as liquidity if the token/WETH price is <= amountTokenDesired/msg.value (WETH depreciates)
amountETHMinuintbounds the extent to which the WETH/token price can go up before the transaction reverts. Must be <= amountTokenDesire
toaddressrecipient of the liquidity tokens
deadlineuintunix timestamp after which the transaction will revert

Returns

NameTypeDescription
amountTokenuintamount of token sent to the pair
amountETHuintamount of ETH converted to WETH and sent to the pool
liquidityuintamount of liquidity tokens minted

removeLiquidity

function removeLiquidity(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB);

Removes liquidity from an ERC-20⇄ERC-20 pool.

  • msg.sender should have already given the router an allowance of at least liquidity on the pool.
  • amountAMin and amountBMin can be used for slippage protection.
  • deadline is used to set a time restriction on how long it can take for the tx to be executed.

Parameters

NameTypeDescription
tokenAaddressaddress for one of the tokens in the pair
tokenBaddressaddress for the other token in the pair
liquidityuintamount of liquidity tokens to remove or burn
amountAMinuintminimum amount of tokenA that must be received for the transaction not to revert
amountBMinuintminimum amount of tokenB that must be received for the transaction not to revert
toaddressrecipient of the underlying assets
deadlineuintunix timestamp after which the transaction will revert

Returns

NameTypeDescription
amountAuintamount of tokenA received
amountBuintamount of tokenB received

removeLiquidityETH

function removeLiquidityETH(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountToken, uint amountETH);

Removes liquidity from an ERC-20⇄WETH pool and receive ETH.

  • msg.sender should have already given the router an allowance of at least liquidity on the pool.
  • amountTokenMin and amountETHMin can be used for slippage protection.
  • deadline is used to set a time restriction on how long it can take for the tx to be executed.

Parameters

NameTypeDescription
tokenaddressother token besides ETH in the pair
liquidityuintamount of liquidity tokens to remove or burn
amountTokenMinuintminimum amount of token that must be received for the transaction not to revert
amountETHMinuintminimum amount of ETH that must be received for the transaction not to revert
touintrecipient of the underlying assets
deadlineuintunix timestamp after which the transaction will revert

Returns

NameTypeDescription
amountTokenuintamount of token received
amountETHuintamount of ETH received

removeLiquidityWithPermit

function removeLiquidityWithPermit(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountA, uint amountB);

Removes liquidity from an ERC-20⇄ERC-20 pool without pre-approval.

  • amountAMin and amountBMin can be used for slippage protection.
  • deadline is used to set a time restriction on how long it can take for the tx to be executed.

Parameters

NameTypeDescription
tokenAaddressaddress for one of the tokens in the pair
tokenBaddressaddress for the other token in the pair
liquidityuintamount of liquidity tokens to remove
amountAMinuintminimum amount of tokenA that must be received for the transaction not to revert
amountBMinuintminimum amount of tokenB that must be received for the transaction not to revert
toaddressrecipient of the underlying assets
deadlineuintunix timestamp after which the transaction will revert
approveMaxboolwhether or not the approval amount in the signature is for liquidity or uint(-1)
vuint8v component of the permit signature
rbytes32r component of the permit signature
sbytes32s component of the permit signature

Returns

NameTypeDescription
amountAuintamount of tokenA received
amountBuintamount of tokenB received

removeLiquidityETHWithPermit

function removeLiquidityETHWithPermit(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountToken, uint amountETH);

Removes liquidity from an ERC-20⇄WETTH pool and receive ETH without pre-approval.

  • amountAMin and amountBMin can be used for slippage protection.
  • deadline is used to set a time restriction on how long it can take for the tx to be executed.

Parameters

NameTypeDescription
tokenaddressother token besides WETH in the pair
liquidityuintamount of liquidity tokens to remove
amountTokenMinuintminimum amount of token that must be received for the transaction not to revert
amountETHMinuintminimum amount of ETH that must be received for the transaction not to revert
toaddressrecipient of the underlying assets
deadlineuintunix timestamp after which the transaction will revert
approveMaxboolwhether or not the approval amount in the signature is for liquidity or uint(-1)
vuint8v component of the permit signature
rbytes32r component of the permit signature
sbytes32s component of the permit signature

Returns

NameTypeDescription
amountTokenuintamount of token received
amountETHuintamount of ETH received

removeLiquidityETHSupportingFeeOnTransferTokens

function removeLiquidityETHSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountETH);

Identical to removeLiquidityETH, but succeeds for tokens that take a fee on transfer.

Parameters

NameTypeDescription
tokenaddressother token besides WETH in the pair
liquidityuintamount of liquidity tokens to remove
amountTokenMinuintminimum amount of token that must be received for the transaction not to revert
amountETHMinuintminimum amount of ETH that must be received for the transaction not to revert
toaddressrecipient of the underlying assets
deadlineuintunix timestamp after which the transaction will revert

Returns

NameTypeDescription
amountETHuintamount of ETH received

removeLiquidityETHWithPermitSupportingFeeOnTransferTokens

function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountETH);

Identical to removeLiquidityETHWithPermit, but succeeds for tokens that take a fee on transfer.

Parameters

NameTypeDescription
tokenaddressother token besides WETH in the pair
liquidityuintthe amount of liquidity tokens to remove
amountTokenMinuintminimum amount of token that must be received for the transaction not to revert
amountETHMinuintminimum amount of ETH that must be received for the transaction not to revert
toaddressrecipient of the underlying assets
deadlineuintunix timestamp after which the transaction will revert
approveMaxboolwhether or not the approval amount in the signature is for liquidity or uint(-1)
vuint8v component of the permit signature
rbytes32r component of the permit signature
sbytes32s component of the permit signature

Returns

NameTypeDescription
amountETHuintThe amount of ETH received.

swapExactTokensForTokens

function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);

Swaps an exact amount of input tokens for as many output tokens as possible, along the route determined by the path. The first element of path is the input token, the last is the output token, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

  • path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity.
  • msg.sender should have already given the router an allowance of at least amountIn on the input token.
  • amountOutMin can be used for slippage protection.
  • deadline is used to set a time restriction on how long it can take for the tx to be executed.

Parameters

NameTypeDescription
amountInuintamount of input tokens to send
amountOutMinuintminimum amount of output tokens that must be received for the transaction not to revert
pathaddress[] calldataarray of token addresses
toaddressrecipient of the output tokens
deadlineuintunix timestamp after which the transaction will revert

Returns

NameTypeDescription
amountsuint[] memoryThe input token amount and all subsequent output token amounts.

swapTokensForExactTokens

function swapTokensForExactTokens(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);

Receive an exact amount of output tokens for as few input tokens as possible, along the route determined by the path. The first element of path is the input token, the last is the output token, and any intermediate elements represent intermediate tokens to trade through (if, for example, a direct pair does not exist).

  • path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity.
  • msg.sender should have already given the router an allowance of at least amountIn on the input token.
  • amountInMax can be used for slippage protection.
  • deadline is used to set a time restriction on how long it can take for the tx to be executed.

Parameters

NameTypeDescription
amountOutuintamount of output tokens to receive
amountInMaxuintmaximum amount of input tokens that can be required before the transaction reverts
pathaddress[] calldataarray of token addresses
toaddressrecipient of the output tokens
deadlineuintunix timestamp after which the transaction will revert

Returns

NameTypeDescription
amountsuint[] memoryinput token amount and all subsequent output token amounts

swapExactETHForTokens

function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
external
payable
returns (uint[] memory amounts);

Swaps an exact amount of ETH for as many output tokens as possible, along the route determined by the path. The first element of path must be WETH, the last is the output token, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

  • path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity.
  • amountOutMin can be used for slippage protection.
  • deadline is used to set a time restriction on how long it can take for the tx to be executed.

Parameters

NameTypeDescription
msg.value (amountIn)uintamount of ETH to send
amountOutMinuintminimum amount of output tokens that must be received for the transaction not to revert
pathaddress[] calldataarray of token addresses
toaddressrecipient of the output tokens
deadlineuintunix timestamp after which the transaction will revert

Returns

NameTypeDescription
amountsuint[] memoryinput token amount and all subsequent output token amounts

swapTokensForExactETH

function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
external
returns (uint[] memory amounts);

Receive an exact amount of ETH for as few input tokens as possible, along the route determined by the path. The first element of path is the input token, the last must be WETH, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

  • msg.sender should have already given the router an allowance of at least amountInMax on the input token.
  • If the to address is a smart contract, it must have the ability to receive ETH.
  • path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity.
  • amountInMax can be used for slippage protection.
  • deadline is used to set a time restriction on how long it can take for the tx to be executed.

Parameters

NameTypeDescription
amountOutuintamount of ETH to receive
amountInMaxuintmaximum amount of input tokens that can be required before the transaction reverts
pathaddress[] calldataarray of token addresses
toaddressrecipient of ETH
deadlineuintunix timestamp after which the transaction will revert

Returns

NameTypeDescription
amountsuint[] memoryinput token amount and all subsequent output token amounts

swapExactTokensForETH

function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
external
returns (uint[] memory amounts);

Swaps an exact amount of tokens for as much ETH as possible, along the route determined by the path. The first element of path is the input token, the last must be WETH, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

  • If the to address is a smart contract, it must have the ability to receive ETH.
  • path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity.
  • amountOutMin can be used for slippage protection.
  • deadline is used to set a time restriction on how long it can take for the tx to be executed.

Parameters

NameTypeDescription
amountInuintamount of input tokens to send
amountOutMinuintminimum amount of output tokens that must be received for the transaction not to revert
pathaddress[] calldataarray of token addresses
toaddressrecipient of the ETH
deadlineuintunix timestamp after which the transaction will revert

Returns

NameTypeDescription
amountsuint[] memoryinput token amount and all subsequent output token amounts

swapETHForExactTokens

function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
external
payable
returns (uint[] memory amounts);

Receive an exact amount of tokens for as little ETH as possible, along the route determined by the path. The first element of path must be WETH, the last is the output token and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

  • Leftover ETH, if any, is returned to msg.sender.
  • path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity.
  • deadline is used to set a time restriction on how long it can take for the tx to be executed.

Parameters

NameTypeDescription
amountOutuintamount of tokens to receive
msg.value (amountInMax)uintmaximum amount of ETH that can be required before the transaction reverts
pathaddress[] calldataarray of token addresses
toaddressrecipient of the output tokens
deadlineuintunix timestamp after which the transaction will revert

Returns

NameTypeDescription
amountsuint[] memoryinput token amount and all subsequent output token amounts

swapExactTokensForTokensSupportingFeeOnTransferTokens

function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;

Identical to swapExactTokensForTokens, but succeeds for tokens that take a fee on transfer.

  • msg.sender should have already given the router an allowance of at least amountIn on the input token.

Parameters

NameTypeDescription
amountInuintamount of input tokens to send.
amountOutMinuintminimum amount of output tokens that must be received for the transaction not to revert.
pathaddress[] calldataarray of token addresses
toaddressrecipient of the output tokens
deadlineuintunix timestamp after which the transaction will revert

swapExactETHForTokensSupportingFeeOnTransferTokens

function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable;

Identical to swapExactETHForTokens, but succeeds for tokens that take a fee on transfer.

Parameters

NameTypeDescription
msg.value (amountIn)uintamount of ETH to send
amountOutMinuintminimum amount of output tokens that must be received for the transaction not to revert
pathaddress[] calldataarray of token addresses
toaddressrecipient of the output tokens
deadlineuintunix timestamp after which the transaction will revert

swapExactTokensForETHSupportingFeeOnTransferTokens

function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;

Identical to swapExactTokensForETH, but succeeds for tokens that take a fee on transfer.

  • If the to address is a smart contract, it must have the ability to receive ETH.

Parameters

NameTypeDescription
amountInuintamount of input tokens to send
amountOutMinuintminimum amount of output tokens that must be received for the transaction not to revert
pathaddress[] calldataarray of token addresses. path.length must be >= 2
toaddressrecipient of the ETH
deadlineuintunix timestamp after which the transaction will revert

Interface

pragma solidity >=0.6.2;

import './IUniswapV2Router01.sol';

interface IUniswapV2Router02 is IUniswapV2Router01 {
function removeLiquidityETHSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountETH);
function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountETH);

function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable;
function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
}

ABI